package com.example.httpdemo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.httpdemo.http.ApiServiceHelper;
import com.example.httpdemo.http.NetworkHelper;
import com.example.httpdemo.model.CombinedData;
import com.example.httpdemo.model.SmsRequest;
import com.example.httpdemo.net.ApiService;


import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.Disposable;


public class MainActivity extends AppCompatActivity {

    private Disposable disposable;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        TextView tvHello = findViewById(R.id.tv_hello);
        tvHello.setOnClickListener(v -> {
            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();
            getData();
        });

    }

    @SuppressLint("CheckResult")
    private void getData() {
        // 创建 API 服务实例
        ApiService apiService = ApiServiceHelper.getApiService();



        SmsRequest request = new SmsRequest("86", "13839131251");
        Single.zip(apiService.getSms(request)
                , apiService.getSms1(request)
                , (messageModel, messageModel2)
                        -> new CombinedData(messageModel.getMessage(),messageModel2.getMessage()))
                .subscribe(
                result -> {
                    // 处理成功结果
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                },
                error -> {
                    // 处理错误
                    Toast.makeText(this, error.getMessage(), Toast.LENGTH_SHORT).show();
                }
        );
        // 发起网络请求
        disposable = NetworkHelper.request(
                apiService.getSms(request),
                model -> {
                    // 处理成功结果
                    Log.d("User", model.getMessage());
                },
                errorMessage -> {
                    // 处理错误
                    Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
                }
        );

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}